package LayoutStudy;

import sample.SimpleLayout;

import javax.swing.*;
import java.awt.*;

public class MyFame2 extends JFrame {
    public MyFame2(String title) throws HeadlessException {
        super(title);
        //panel内容窗格
        JPanel root = new JPanel();
        this.setContentPane(root);

        //设置布局器,采用手工布局root.setLayout(null);
        //自定义布局器LayoutManage2
        //LayoutManage2是新版接口，继承于LayoutManage
        root.setLayout(new SimpleLayout());

        //添加标签
        ColorLabel a1 = new ColorLabel("1", Color.YELLOW);
        ColorLabel a2 = new ColorLabel("2", Color.GREEN);
        ColorLabel a3 = new ColorLabel("3", Color.LIGHT_GRAY);
        ColorLabel a4 = new ColorLabel("4", Color.CYAN);

        root.add(a1);//通知SimpleLayout,执行layout.addLayoutComponent(a1,···);
        root.add(a2);
        root.add(a3);
        root.add(a4);


//        root.add(a1,BorderLayout.NORTH);
//        root.add(a2,BorderLayout.SOUTH);
//        root.add(a3,BorderLayout.EAST);
//        root.add(a4,BorderLayout.WEST);

//        a1.setBounds(0,0,100,50);
//        等价于a1.setLocation(0,0)
//        a1.setSize(a1.getPreferredSize());

    }

    //静态内部类
    private static class ColorLabel extends JLabel {
        public ColorLabel(String title, Color byColor) {
            this.setText(title);
            this.setOpaque(true);
            this.setBackground(byColor);
            this.setPreferredSize(new Dimension(60, 30));
            this.setHorizontalAlignment(SwingConstants.CENTER);
        }

    }
}
